import Vue from 'vue'
import Router from 'vue-router'
import {getCookie} from "../js/common/util";
import instance from '../api/index'
import layout from '@/pages/home'
import store from '@/store'

Vue.use(Router)

export const constantRouterMap = [
  {
    path: '/login',
    name: '登录',
    component: (revoke) => require(['@/pages/login'], revoke),
    children: [
      {
        path: '/login',
        name: '登录',
        component: (revoke) => require(['@/pages/login'], revoke)
      }
    ]
  },
  // {
  //   path: '/notFound',
  //   name: '页面未找到',
  //   component: (revoke) => require(['@/pages/notFound'], revoke),
  //   children: [
  //     {
  //       path: '/notFound',
  //       name: '页面未找到',
  //       component: (revoke) => require(['@/pages/notFound'], revoke)
  //     }
  //   ]
  // },
  {
    path: '/401',
    name: '未找到服务器',
    component: (revoke) => require(['@/pages/serverError'], revoke),
    children: [
      {
        path: '/401',
        name: '未找到服务器',
        component: (revoke) => require(['@/pages/serverError'], revoke),
      }
    ]
  },
  {
    path: '/register',
    name: '注册页面',
    component: (revoke) => require(['@/pages/register'], revoke),
    children: [
      {
        path: '/register',
        name: '注册页面',
        component: (revoke) => require(['@/pages/register'], revoke),
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/',
        name: '系统首页',
        component: (revoke) => require(['@/pages/theSystemFrontPage/systemPage/systemPage'], revoke),
      }
    ],
  },
];
let router = new Router({
  linkActiveClass: 'active',
  routes: constantRouterMap
});
export const asyncRouterMap = [
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/user/getEnterpriseInfo',
        name: '企业信息',
        component: (revoke) => require(['@/pages/companyProfile/companyProfile'], revoke),
      }
    ],
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/warehouse/getWareHouseList',
        name: '仓库设置',
        component: (revoke) => require(['@/pages/storage/storageSetting'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: "/v1/warehouse/getStockList",
        name: '库存清单',
        component: (revoke) => require(['@/pages/storage/detailedAccount'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: "/v1/whReceiptProcess/getAllotList",
        name: '仓库调拨',
        component: (revoke) => require(['@/pages/storage/storageAllot'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: "/v1/whReceiptProcess/getWareHouserCheckList",
        name: '仓库盘点',
        component: (revoke) => require(['@/pages/storage/inventory'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: "/v1/whReceiptProcess/getWareHouserOutList",
        name: '出库管理',
        component: (revoke) => require(['@/pages/storage/warehouseManagement'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: "/v1/whReceiptProcess/getWareHouserInList",
        name: '入库管理',
        component: (revoke) => require(['@/pages/storage/stockManagement'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/goods/getList',
        name: '商品列表',
        component: (revoke) => require(['@/pages/product/CommodityManagement/productList'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/goods/combination/getList',
        name: '组合商品',
        component: (revoke) => require(['@/pages/product/CommodityManagement/goodsOfJoint'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/goods/addOrEdit',
        name: '添加单个SKU',
        component: (revoke) => require(['@/pages/product/CommodityManagement/singleSKU'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/goods/combination/addOrEdit',
        name: '添加组合SKU',
        component: (revoke) => require(['@/pages/product/CommodityManagement/combinationSKU'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/goods/category/getList',
        name: '商品分类',
        component: (revoke) => require(['@/pages/product/ProductConfiguration/ClassificationList'], revoke)
      }
    ]

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/product/AddTheGoods',
        name: '添加商品',
        component: (revoke) => require(['@/pages/product/CommodityManagement/AddTheGoods'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/product/addCommodityClassification',
        name: '添加商品分类',
        component: (revoke) => require(['@/pages/product/ProductConfiguration/addCommodityClassification'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/product/addNextProduct',
        name: '添加商品详情',
        component: (revoke) => require(['@/pages/product/CommodityManagement/addNextProduct'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/goods/brand/getList',
        name: '品牌管理',
        component: (revoke) => require(['@/pages/product/ProductConfiguration/BrandManagement'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/product/CommodityAttribute',
        name: '商品属性',
        component: (revoke) => require(['@/pages/product/ProductConfiguration/CommodityAttribute'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/product/AddAttributes',
        name: '添加商品属性',
        component: (revoke) => require(['@/pages/product/ProductConfiguration/AddAttributes'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/goodsRelation',
        name: '添加商品属性',
        component: (revoke) => require(['@/pages/product/ProductRelation'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/author/getShoplist',
        name: '店铺列表',
        component: (revoke) => require(['@/pages/setUpThe/generalChannelManagement/listOfAuthorizedStores'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/author/getPlatFrom',
        name: '未授权店铺列表',
        component: (revoke) => require(['@/pages/setUpThe/generalChannelManagement/theStoreList'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/role/getUserRole',
        name: '角色管理',
        component: (revoke) => require(['@/pages/setUpThe/rightsManagement/roleManagement'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/user/getUserList/',
        name: '用户管理',
        component: (revoke) => require(['@/pages/setUpThe/rightsManagement/userManagement'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/publish',
        name: '产品列表',
        component: (revoke) => require(['@/pages/publish/ProductLibrary/PublishProductList'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/publish/PublishProductList',
        name: '产品列表',
        component: (revoke) => require(['@/pages/publish/ProductLibrary/PublishProductList'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/ebay/goods/getList',
        name: 'eBay商品',
        component: (revoke) => require(['@/pages/publish/EBayPublished/Draft'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/ebay/draft/edit/getData',
        name: '添加草稿',
        component: (revoke) => require(['@/pages/publish/EBayPublished/AddDraft'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/order/list',
        name: '订单列表',
        component: (revoke) => require(['@/pages/order/orderList'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/order/detail',
        name: '订单详情',
        component: (revoke) => require(['@/pages/order/orderDetailPage'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/declare/crossBorder/detail',
        name: '订单详情',
        component: (revoke) => require(['@/pages/order/borderDetailPage'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/order/log/list',
        name: '同步日志',
        component: (revoke) => require(['@/pages/order/synchronizationLog'], revoke)
      }
    ],
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/order/log',
        name: '同步日志查询',
        component: (revoke) => require(['@/pages/order/logDetail'], revoke)
      }
    ],
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/order/exprotOrder',
        name: '导出到U8',
        component: (revoke) => require(['@/pages/order/exprotOrder'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/order/orderDetail',
        name: '订单详情',
        component: (revoke) => require(['@/pages/order/orderDetail'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/order/orderDetail1',
        name: '订单详情',
        component: (revoke) => require(['@/pages/order/orderInfo'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/order/create',
        name: '手工创建订单',
        component: (revoke) => require(['@/pages/order/manuallyCreateOrder'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/declare/crossBorder/query/create',
        name: '手工创建订单',
        component: (revoke) => require(['@/pages/order/borderCreateOrder'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/declare/query',
        name: '报关单管理',
        component: (revoke) => require(['@/pages/order/customsDeclaration'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/declare/crossBorder/query',
        name: '跨境申报',
        component: (revoke) => require(['@/pages/order/crossBorder'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/declare/record/list',
        name: '备案管理',
        component: (revoke) => require(['@/pages/order/recordManagement'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/declare/record',
        name: '提交备案',
        component: (revoke) => require(['@/pages/order/submittRecord'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/order/orderDeclareRelation/getList',
        name: '国收申报',
        // component: (revoke) => require(['@/pages/receipt/chargeDeclare'], revoke)
        component: (revoke) => require(['@/pages/receipt/chargeDeclareList'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/icbc/blackPreCheckResponse/getList',
        name: '黑名单列表',
        component: (revoke) => require(['@/pages/receipt/blackList'], revoke)
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/v1/icbc/orderPaymentResponse/getList',
        name: '支付申请列表',
        component: (revoke) => require(['@/pages/receipt/paymentAuditList'], revoke)
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/paypal/paypalList',
        name: 'PayPal账号管理',
        component: (revoke) => require(['@/pages/financial/payPalAccountManagement'], revoke)
      }
    ],

  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/financial/inTheManagement',
        name: '收汇管理',
        component: (revoke) => require(['@/pages/financial/inTheManagement'], revoke)
      }
    ],
  },
  {
    path: '*',
    name: '页面未找到',
    component: (revoke) => require(['@/pages/notFound'], revoke),
    children: [
      {
        path: '',
        name: '页面未找到',
        component: (revoke) => require(['@/pages/notFound'], revoke)
      }
    ]
  },
  {
    path: '/notFound',
    name: '页面未找到',
    component: (revoke) => require(['@/pages/notFound'], revoke),
    children: [
      {
        path: '/notFound',
        name: '页面未找到',
        component: (revoke) => require(['@/pages/notFound'], revoke)
      }
    ]
  },
];

router.beforeEach((to, from, next) => {
  let routerPath = to.meta.path || to.path;
  let userInfo = getCookie('token');
  if (userInfo) {
    if (store.getters.addRouters.length < 1) {
      store.dispatch('GenerateRoutes', userInfo).then(res => {
        sessionStorage.setItem("asyncRouterMap", JSON.stringify(store.getters.addRouters));
        router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
        next(to.path);
      })
      next()

    } else {
      let flag = false;
      for (let i = 0; i < store.getters.routers.length; i++) {
        if (store.getters.routers[i].children[0].path == to.path) {
          flag = true;
        }
      }
      if (flag) {
        next();
      } else {
        alert('您没有权限，请重新登录！！！');
        next({
          path: '/login'
        })
      }
      // next();
    }
  } else {
    if (to.path == '/login') {//如果是登录页面路径，就直接next()
      next();
    } else {//不然就跳转到登录；
      next({
        path: '/login'
        // query:{redirect:router.currentRoute.fullPath}// 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
});

export default router;

